package com.baobao.service;

import com.baobao.dao.BanjiDAO;

import com.baobao.domain.Banji;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for Banji entities
 * 
 */

@Service("BanjiService")
@Transactional
public class BanjiServiceImpl implements BanjiService {

	/**
	 * DAO injected by Spring that manages Banji entities
	 * 
	 */
	@Autowired
	private BanjiDAO banjiDAO;

	/**
	 * Instantiates a new BanjiServiceImpl.
	 *
	 */
	public BanjiServiceImpl() {
	}

	/**
	 */
	@Transactional
	public Banji findBanjiByPrimaryKey(String id) {
		return banjiDAO.findBanjiByPrimaryKey(id);
	}

	/**
	 * Save an existing Banji entity
	 * 
	 */
	@Transactional
	public void saveBanji(Banji banji) {
		Banji existingBanji = banjiDAO.findBanjiByPrimaryKey(banji.getId());

		if (existingBanji != null) {
			if (existingBanji != banji) {
				existingBanji.setId(banji.getId());
				existingBanji.setSchoolId(banji.getSchoolId());
				existingBanji.setName(banji.getName());
				existingBanji.setMasterName(banji.getMasterName());
				existingBanji.setMasterPhone(banji.getMasterPhone());
			}
			banji = banjiDAO.store(existingBanji);
		} else {
			banji = banjiDAO.store(banji);
		}
		banjiDAO.flush();
	}

	/**
	 * Return a count of all Banji entity
	 * 
	 */
	@Transactional
	public Integer countBanjis() {
		return ((Long) banjiDAO.createQuerySingleResult("select count(o) from Banji o").getSingleResult()).intValue();
	}

	/**
	 * Load an existing Banji entity
	 * 
	 */
	@Transactional
	public Set<Banji> loadBanjis() {
		return banjiDAO.findAllBanjis();
	}

	/**
	 * Delete an existing Banji entity
	 * 
	 */
	@Transactional
	public void deleteBanji(Banji banji) {
		banjiDAO.remove(banji);
		banjiDAO.flush();
	}

	/**
	 * Return all Banji entity
	 * 
	 */
	@Transactional
	public List<Banji> findAllBanjis(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<Banji>(banjiDAO.findAllBanjis(startResult, maxRows));
	}
}
